package com.chinacoal.microservice.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.mapper.CcmsSmartleasingIndicatorMapper;
import com.chinacoal.microservice.pojo.CcmsSmartleasingIndicator;
import com.chinacoal.microservice.pojo.Currency;
import com.chinacoal.microservice.service.IndicatorService;
import com.chinacoal.microservice.util.Result;

/**
 * 考核模块-指标管理业务实现层
 * @author wiilead
 *
 */
@Service
public class IndicatorServiceImpl implements IndicatorService {
	@Autowired
	private CcmsSmartleasingIndicatorMapper ccmsSmartleasingIndicatorMapper;
	
	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 新增或修改指标
	 */
	@Override
	public Result addOrUpdateIndicator(CcmsSmartleasingIndicator indicator, String token) {
		// TODO Auto-generated method stub
		//得到登录人id,并做兼容性处理
        Currency currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getSignature());
        if (null == currency) {
            currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getClaim("phone").asString());
        }
        
        if(StringUtils.isEmpty(indicator.getIndicatorId())) { //新增
        	indicator.setIndicatorId(UUID.randomUUID() + "");
        	indicator.setCreatedBy(currency.getLoginId());
            indicator.setCreationDate(new Date());
            indicator.setLastUpdatedBy(currency.getLoginId());
            indicator.setLastUpdateDate(new Date());
            
            ccmsSmartleasingIndicatorMapper.insert(indicator);
        }else{ //修改
        	indicator.setLastUpdatedBy(currency.getLoginId());
            indicator.setLastUpdateDate(new Date());
            
            ccmsSmartleasingIndicatorMapper.updateByIndicatorId(indicator);
        }
        
		return Result.success();
	}


	/**
	 * 查询指标列表
	 */
	@Override
	public Result<List<CcmsSmartleasingIndicator>> getIndicatorList() {
		// TODO Auto-generated method stub
		
		List<CcmsSmartleasingIndicator> indicatorList = ccmsSmartleasingIndicatorMapper.selectIndicatorList();
		
		//一级指标
		List<CcmsSmartleasingIndicator> parentIndictorList = new ArrayList<CcmsSmartleasingIndicator>();
		for(CcmsSmartleasingIndicator indicator:indicatorList) {
			if("0".equals(indicator.getParentId())) {
				//二级指标
				List<CcmsSmartleasingIndicator> childIndicatorList = new ArrayList<CcmsSmartleasingIndicator>();
				for(CcmsSmartleasingIndicator childIndicator:indicatorList) {
					if(childIndicator.getParentId().equals(indicator.getIndicatorId())) {
						childIndicatorList.add(childIndicator);
					}
				}
				indicator.setChildIndicatorList(childIndicatorList);
				parentIndictorList.add(indicator);
			}
		}
		
		return Result.success(parentIndictorList);
	}

	/**
	 * 删除指标库
	 */
	@Override
	public Result deleteIndicator(String indicatorId) {
		// TODO Auto-generated method stub
		ccmsSmartleasingIndicatorMapper.deleteIndicator(indicatorId);
		return Result.success();
	}

}
